/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.scuecsz.model.bussiness;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.scuecsz.model.to.ADMIN;
import org.scuecsz.model.to.USER_MAIN;
import org.scuecsz.utils.ConnectionBuilder;
import org.scuecsz.utils.DBHandler;
import org.scuecsz.utils.FileUtil;
import org.scuecsz.view.utils.ImageData;

/**
 *
 * @author Administrator
 */
public class RegisterLoginModel {

    public static int regist(USER_MAIN userMain, ImageData data) throws SQLException, Exception {
        int count = 0;
        Connection conn = null;
        PreparedStatement pstmt = null;
        FileInputStream fis = null;
        try {

            conn = ConnectionBuilder.getConnection();
            //插入校友基本数据到数据库
            count = DBHandler.insert(userMain, conn);
            if (count != 0) {
                //插入校友毕业证图片到数据库
//                fis = new FileInputStream(file);
                pstmt = conn.prepareStatement("insert into DIPLOMA (USER_ID,DIPLOMA,EXTENSION,DATE) values (?,?,?,now())");
                pstmt.setString(1, userMain.getUSER_ID());
                pstmt.setBytes(2, data.getData());
//                pstmt.setBinaryStream(2, fis, (int) file.length());
                pstmt.setString(3, data.getExtension());
                count = pstmt.executeUpdate();
                if (count == 0) {
                    conn.rollback();
                }
            }
        } catch (SQLException e) {
            throw e;
        } catch (Exception e) {
            conn.rollback();
            throw e;
        } finally {
            ConnectionBuilder.closePreparedStatement(pstmt);
            ConnectionBuilder.closeConnection(conn);
            FileUtil.closeFileInputStream(fis);
        }
        return count;
    }

    public static USER_MAIN login(String userID) throws SQLException, Exception {
        USER_MAIN userMain = null;
        Connection conn = null;
        try {
            conn = ConnectionBuilder.getConnection();
            List values = new ArrayList();
            values.add(userID);
            userMain = (USER_MAIN) DBHandler.queryOne("org.scuecsz.model.to.USER_MAIN", "SELECT * FROM USER_MAIN WHERE USER_ID=?", values, conn);
        } catch (SQLException e) {
            throw e;
        } catch (Exception e) {
            conn.rollback();
            throw e;
        } finally {
            ConnectionBuilder.closeConnection(conn);
        }
        return userMain;
    }

    public static String loginRightCheck(USER_MAIN userMain, String password) {
        String errorMsg = null;
        if (userMain == null) {
            errorMsg = "用户ID不存在！";
        } else if (!password.equals(userMain.getPASSWORD())) {
            errorMsg = "密码错误！";
        } else if ("N".equals(userMain.getSTATUS())) {
            errorMsg = "用户没有被激活！";
        }
        return errorMsg;
    }

    public static ADMIN admlogin(String userID) throws SQLException, Exception {
        ADMIN admin = null;
        Connection conn = null;
        try {
            conn = ConnectionBuilder.getConnection();
            List values = new ArrayList();
            values.add(userID);
            admin = (ADMIN) DBHandler.queryOne("org.scuecsz.model.to.ADMIN", "SELECT * FROM ADMIN WHERE ADMIN_ID=?", values, conn);
        } catch (SQLException e) {
            throw e;
        } catch (Exception e) {
            conn.rollback();
            throw e;
        } finally {
            ConnectionBuilder.closeConnection(conn);
        }
        return admin;
    }

    public static String adminRightCheck(ADMIN admin, String password) {
        String errorMsg = null;
        if (admin == null) {
            errorMsg = "ID不存在！";
        } else if (!password.equals(admin.getPASSWORD())) {
            errorMsg = "密码错误！";
        } else if ("N".equals(admin.getSTATUS())) {
            errorMsg = "此管理员已经被锁定！";
        }
        return errorMsg;
    }
}
